package net.xxcxcxcx.xc_enhancedmob.render.damage_num;

import com.mojang.blaze3d.systems.RenderSystem;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.Camera;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.text.LiteralText;
import net.minecraft.util.Formatting;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.math.Vec3f;
import net.xxcxcxcx.xc_enhancedmob.config.CustomConfig;
import net.xxcxcxcx.xc_enhancedmob.config.render.DamageNumConfig;
import net.xxcxcxcx.xc_enhancedmob.config.render.RenderConfig;
import net.xxcxcxcx.xc_enhancedmob.register.ConfigRegister;
import net.xxcxcxcx.xc_enhancedmob.render.HudRendererHelper;
import net.xxcxcxcx.xc_enhancedmob.render.TextureElement;

import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 用来渲染伤害数字
 */
@Environment(value = EnvType.CLIENT)
public class DamageNumRenderer {
    public static CopyOnWriteArrayList<DamageNumParticle> PARTICLES = new CopyOnWriteArrayList();
    private static int tickCount = 0;

    public DamageNumRenderer() {
    }

    public static synchronized void renderParticles(MatrixStack matrix, Camera camera) {
        Iterator var2 = PARTICLES.iterator();
        while (var2.hasNext()) {
            DamageNumParticle p = (DamageNumParticle) var2.next();
            renderParticle(matrix, p, camera);
        }
    }

    private static void renderParticle(MatrixStack matrix, DamageNumParticle particle, Camera camera) {
        float scaleToGui = 0.025F;
        MinecraftClient client = MinecraftClient.getInstance();
        float tickDelta = client.getTickDelta();
        double x = MathHelper.lerp(tickDelta, particle.xPrev, particle.x);
        double y = MathHelper.lerp(tickDelta, particle.yPrev, particle.y);
        double z = MathHelper.lerp(tickDelta, particle.zPrev, particle.z);
        Vec3d camPos = camera.getPos();
        double camX = camPos.x;
        double camY = camPos.y;
        double camZ = camPos.z;
        matrix.push();
        matrix.translate(x - camX, y - camY, z - camZ);//此处进行了一个坐标变化
        matrix.multiply(Vec3f.POSITIVE_Y.getDegreesQuaternion(-camera.getYaw()));
        matrix.multiply(Vec3f.POSITIVE_X.getDegreesQuaternion(camera.getPitch()));
        matrix.scale(-scaleToGui, -scaleToGui, scaleToGui);
        RenderSystem.enableDepthTest();
        RenderSystem.enableBlend();
        RenderSystem.blendFuncSeparate(770, 771, 1, 0);
        drawDamageNumber(matrix, particle.damage, MinecraftClient.getInstance().textRenderer.fontHeight, 0.0f, particle.damageType);
        RenderSystem.disableBlend();
        RenderSystem.enableDepthTest();
        RenderSystem.enableBlend();
        RenderSystem.blendFuncSeparate(770, 771, 1, 0);
        drawDamageIcon(matrix, 0.0f, 0.0f, particle);
        RenderSystem.disableBlend();
        matrix.pop();
    }

    public static void drawDamageIcon(MatrixStack matrix, float x, float y, DamageNumParticle particle) {
        int cordX = 0, cordY = 0;
        CustomConfig config = ConfigRegister.getConfig(new RenderConfig().getName());
        if (config instanceof RenderConfig renderConfig) {
            DamageNumConfig damageNumConfig = renderConfig.getDamageNumConfig(particle.damageType);
            cordX = damageNumConfig.getIconCordX();
            cordY = damageNumConfig.getIconCordY();
        }
        TextureElement damageIcon = new TextureElement("xc_enhancedmob:textures/gui/damage_icon/damage_icon_tex.png", cordX * 16, cordY * 16, 16, 16, 256, 256);

        MinecraftClient.getInstance().getTextureManager().bindTexture(damageIcon.getTexture());
        float x0 = x - 7 / 2.0f,
                x1 = x + 7 / 2.0f,
                y0 = y - 7 / 2.0f,
                y1 = y + 7 / 2.0f;
        HudRendererHelper.drawTexture(matrix, damageIcon, x0, x1, y0, y1, 0, 1f, 1f);
    }

    public static void drawDamageNumber(MatrixStack matrix, float dmg, float x, float y, String damageType) {
        LiteralText s = new LiteralText(String.format("%.2f", dmg));
        s = setTextColorByType(s, damageType);
        MinecraftClient minecraft = MinecraftClient.getInstance();
        minecraft.textRenderer.draw(matrix, s, x - 7 / 2.0f, y - 7 / 2.0f, 0);
    }

    //update every tick
    public static void tick() {
        PARTICLES.forEach((p) -> {
            p.tick();
        });
        PARTICLES.removeIf((p) -> p.age > 50);
        ++tickCount;
    }

    private static LiteralText setTextColorByType(LiteralText s, String damageType) {
        CustomConfig config = ConfigRegister.getConfig(new RenderConfig().getName());
        if (config instanceof RenderConfig renderConfig) {
            DamageNumConfig damageNumConfig = renderConfig.getDamageNumConfig(damageType);
            return (LiteralText) s.formatted(damageNumConfig.getColor());
        }
        return (LiteralText) s.formatted(Formatting.RED);
    }
}
